<?php

namespace app\data\controller;

use app\data\service\UserService;
use think\admin\Controller;
use think\cache\driver\Redis;

/**
 * 普通用户管理
 * Class User
 * @package app\data\controller
 */
class User extends Controller
{
    /**
     * 绑定数据表
     * @var string
     */
    private $table = 'DataUser';

    /**
     * 普通用户管理
     * @auth true
     * @menu true
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function index()
    {
        $this->title = '普通用户管理';
        $query = $this->_query($this->table);
        $query->like('phone,username|nickname#username');
        $query->whereRaw('nickname != "" or username != ""');
        $query->order('id desc')->equal('status')->where("deleted",0)->dateBetween('create_at')->page();
    }

    /**
     * 数据列表处理
     * @param array $data
     */
    protected function _page_filter(array &$data)
    {
        UserService::instance()->buildByUid($data, 'from', 'fromer');
    }

    public function setvip(){
        $this->title = '添加箱格';
        $this->_form($this->table, 'setvip');
    }

    /**
     * 数据表单处理
     * @param array $data
     * @return array
     */
    protected function _form_filter(array &$data)
    {
        $user_id = input("user_id", 0);
        if($this->app->request->isPost()) {
            $discounts = input("discounts",'');
            if($user_id){
                $res = 1;
                $id = $this->app->db->name("data_user_discounts")->where("user_id", $user_id)->value('id');
                if($discounts=='' && $id){
                    $res = $this->app->db->name("data_user_discounts")->where("id", $id)->delete();
                }else if($discounts!='' && $id){
                    $res = $this->app->db->name("data_user_discounts")->where("id", $id)->update(['user_id'=>$user_id, 'discounts'=>$discounts]);
                }else if($discounts!=''){
                    $res = $this->app->db->name("data_user_discounts")->insertGetId(['user_id'=>$user_id, 'discounts'=>$discounts]);
                }
                if($res){
                    $redis = new Redis();
                    if($discounts!="") $redis->handler()->ZADD("UserDiscounts", $discounts, $user_id);
                    if($discounts=='' && $id){
                        $redis->handler()->ZREM("UserDiscounts", $user_id);
                    }
                    $this->success('成功');
                }else{
                    $this->error('失败');
                }
            }else{
                $this->success('失败');
            }
        }else{
            $data = $this->app->db->name("data_user_discounts")->where("user_id", $user_id)->find();
        }
    }

    /**
     * 修改用户状态
     * @auth true
     * @throws \think\db\exception\DbException
     */
    public function state()
    {
        $this->_save($this->table, $this->_vali([
            'status.in:0,1'  => '状态值范围异常！',
            'status.require' => '状态值不能为空！',
        ]));
    }

    /**
     * 修改用户状态
     * @auth true
     * @throws \think\db\exception\DbException
     */
    public function modify()
    {
        $this->_save($this->table);
    }

}